package com.audaque.springboot.foshanupload.web.datamongodbdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.audaque.springboot.foshanupload.web.datamongodbdemo.api.MusicControllerApi;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.Book;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.Chapter;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.MusicDetail;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.qo.MusicQo;
import com.audaque.springboot.foshanupload.web.datamongodbdemo.repository.MusicDetailRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

/**
 * @author zgb
 * @desc
 * @date 2023-07-23 02:24:25
 */
@RestController
@RequestMapping("/musicDetailRepository")
public class MusicDetailRepositoryController implements MusicControllerApi {

    @Autowired
    private MusicDetailRepository musicDetailRepository;

    @Override
    @PostMapping("/add")
    public String add(@RequestBody MusicDetail musicDetail) {
        musicDetail.setId(null);
        musicDetail.setAddTime(new Date());
        MusicDetail save = musicDetailRepository.save(musicDetail);
        String id = save.getId();
        return id;
    }


    @Override
    @PutMapping("/edit")
    public void edit(@RequestBody MusicDetail musicDetail) throws Exception {
        String id = musicDetail.getId();
        if (StringUtils.isBlank(id)) {
            throw new Exception("id不能为null");
        }
        //先查询
        Optional<MusicDetail> optional = musicDetailRepository.findById(id);
        if (!optional.isPresent()) {
            throw new Exception("根据id查不到该记录");
        }
        MusicDetail byId = optional.get();


        CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setIgnoreNullValue(true);
        copyOptions.setIgnoreProperties("book");
        BeanUtil.copyProperties(musicDetail,byId,copyOptions);

        Book book = musicDetail.getBook();
        Book book1 = byId.getBook();
        copyOptions.setIgnoreProperties("chapter");
        BeanUtil.copyProperties(book,book1,copyOptions);

        Chapter chapter = book.getChapter();
        Chapter chapter1 = book1.getChapter();
        BeanUtil.copyProperties(chapter,chapter1,copyOptions);
        book1.setChapter(chapter1);
        byId.setBook(book1);



        musicDetailRepository.save(byId);
    }

    @Override
    public void editField(String id, String name) throws Exception {

    }

    @Override
    @PutMapping("/override2json")
    public void override2json(String id) throws Exception {

    }

    @Override
    @PutMapping("/override")
    public void override(@RequestBody MusicDetail musicDetail) {
        musicDetailRepository.save(musicDetail);
    }

    @Override
    @GetMapping("/get/{id}")
    public MusicDetail get(@PathVariable("id") String id) {
        Optional<MusicDetail> byId = musicDetailRepository.findById(id);
        return byId.get();
    }

    @Override
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable("id") String id) {
        musicDetailRepository.deleteById(id);
    }

    @Override
    @GetMapping("/page/{page}/{size}")
    public Page<MusicDetail> page(@PathVariable("page") int page, @PathVariable("size") int size, MusicQo musicQo) {
        // 判断查询条件非空
        if (musicQo == null) {
            musicQo = new MusicQo();
        }
        //创建条件值对象
        MusicDetail music = new MusicDetail();
        //按照作者、音乐名称、专辑名模糊查询
        //定义条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().
                withMatcher("author", ExampleMatcher.GenericPropertyMatchers.contains()).
                withMatcher("musicName", ExampleMatcher.GenericPropertyMatchers.contains()).
                withMatcher("album", ExampleMatcher.GenericPropertyMatchers.contains());
        //在条件匹配器中我没有设置musicType这个参数
        //没有设置的话它默认是按精确匹配
        //判断各参数是否为空
        if (StringUtils.isNotEmpty(musicQo.getMusicType())) {
            music.setMusicType(musicQo.getMusicType());
        }
        if (StringUtils.isNotEmpty(musicQo.getMusicName())) {
            music.setMusicName(musicQo.getMusicName());
        }
        if (StringUtils.isNotEmpty(musicQo.getAlbum())) {
            music.setAlbum(musicQo.getAlbum());
        }
        if (StringUtils.isNotEmpty(musicQo.getAuthor())) {
            music.setAuthor(musicQo.getAuthor());
        }
        //基于值对象和匹配器定义条件对象
        Example<MusicDetail> example = Example.of(music, exampleMatcher);
        if (page <= 0) {
            page = 1;
        }
        //page=0代表第1页，所以这里要从0开始
        page = page - 1;

        //分页
        Pageable pageable = PageRequest.of(page, size);
        //获取分页对象
        Page<MusicDetail> all = musicDetailRepository.findAll(example, pageable);

        return all;

    }

    @Override
    public MusicDetail dBRef(String bookId) {
        return null;
    }


}
